home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / memory / veum.zip / TEST67H.C < prev    next >
C/C++ Source or Header  |  1987-01-26  |  6KB  |  279 lines

  1. /*
  2.  
  3.     NOTICE:
  4.  
  5.         This code is placed in the public domain for documentation
  6.         purposes only.  It helps demonstrate the Virtual Expanded 
  7.         Memory Manager (VEMM) system distributed with.
  8.  
  9.     (it also happens to work on a REAL EMM, such as AST's RAMpage!)
  10.  
  11. */
  12.  
  13. /*
  14.  
  15.     TEST67H                Copyright (C) 1987, by Marty Ross
  16.  
  17.  
  18.     This test program assumes that at interrupt 67h there is
  19.     only an EMM-type supervisor installed.
  20.  
  21.     Compiled under Lattice C 2.15   (NON-ANSI, sorry)
  22.     and linked with VEC.OBJ and 
  23.     LIM.OBJ (provided).
  24.  
  25. */
  26.  
  27. #include        <dos.h>
  28. #define            HANDLES        4  /* Should become parameter */
  29. #define            toupper(c)    (((c>='a')&&(c<='z'))?(c&223):c)
  30.  
  31. union REGS regs ; 
  32. struct SREGS sregs ;
  33.  
  34. static unsigned emm_pfba ;        /* Segment address of EMM PFBA */
  35.  
  36. unsigned usedp, freep, totalp ;
  37. unsigned vdevt ;            /* "Virtual EMM DEVice Type" */
  38.  
  39. unsigned thandle[HANDLES] ;        /* "test handles"         */
  40. unsigned tusedp[HANDLES] ;        /* sizes of areas held by "" */
  41.  
  42. main()
  43. {
  44. unsigned i ;
  45.  
  46.                 /* Phase 1: setup */
  47. chkout() ;
  48. getver() ; 
  49. getstatus() ;
  50. getpfba() ; 
  51. getsize() ; 
  52.                 /* Phase 2: allocation */
  53. for (i=0;i<HANDLES;i++) {
  54.     aloc(i) ; 
  55.     prt_map() ;
  56.     }
  57.  
  58. getsize() ; 
  59.  
  60. for (i=0;i<HANDLES;i++) {
  61.     de_aloc(i) ;
  62.     prt_map() ;
  63.     }
  64.  
  65. getsize() ; 
  66. getstatus() ;
  67. more() ;
  68.  
  69.                 /* Phase 3: data access */
  70.  
  71. printf("Example (but FAR from typical) application:\n");
  72. aloc(0);             /* alocate to my slot#0 */
  73. use_emm(0, tusedp[0] / 2) ;    /* Do some I/O with it  */
  74. de_aloc(0) ;            /* Release it           */
  75. printf("Done with EMM test.\n");
  76.  
  77. }
  78.  
  79. /*
  80.  
  81.     chkout:
  82.  
  83.     make sure that there is at least a resemblance to an EMM
  84.     supervisor installed.
  85.  
  86.     If it is a true blue EMM, global 'vdevt' is set to 1.
  87.     if there is an unidentified user procedure at the EMM
  88.     interrupt, a 2 is returned in 'vdevt'.  
  89.  
  90.     If there is no interrupt 0x67 driver installed, exit(9)
  91.     is taken after printing an explanatory msg.
  92.  
  93. */
  94. static chkout() 
  95. {
  96. char *name ;
  97.  
  98. if (emmchk()) {
  99.    vdevt = 1;
  100.    name = "EMM supervisor" ;
  101.    }
  102. else if (vecchk()) {
  103.    vdevt = 2;
  104.    name = "user routine on EMM vector" ;
  105.    }
  106. else {
  107.    printf("There's no software driver for INT 0x67, the EMM vector.\n");
  108.    exit(9);
  109.    }
  110.  
  111. printf("Detected %s.\n",name) ;
  112. }
  113.  
  114. static getstatus()
  115. {
  116. regs.x.ax = 0x4000 ;            /* 40h GET STATUS */
  117. int86(0x67, ®s, ®s) ;
  118. if (regs.h.ah) usrexit(0x40,(int)(regs.h.ah)) ;
  119. else {
  120.    printf("Status OK.\n");
  121.    }
  122. }
  123.  
  124. static getpfba() 
  125. {
  126. regs.x.ax = 0x4100 ;            /* 41h GET PFBA */
  127. int86(0x67, ®s, ®s) ;
  128. if (regs.h.ah) usrexit(0x41,(int)(regs.h.ah)) ;
  129. else {
  130.    printf("PFBA is at %04x.\n",emm_pfba=regs.x.bx);
  131.    }
  132. }
  133.  
  134. static getsize() 
  135. {
  136. regs.x.ax = 0x4200 ;            /* 42H GET SIZE */
  137. int86(0x67, ®s, ®s) ;
  138. if (regs.h.ah) usrexit(0x42,(int)(regs.h.ah)) ;
  139. else {
  140.    printf("Total size is %d pages\n",totalp=regs.x.dx);
  141.    printf("Free size is %d pages\n",freep=regs.x.bx);
  142.    }
  143. }
  144.  
  145. static getver()  
  146. {
  147. regs.x.ax = 0x4600 ;            /* 46h GET VER. */
  148. int86(0x67, ®s, ®s) ;
  149. if (regs.h.ah) usrexit(0x46,(int)(regs.h.ah)) ;
  150. else {
  151.    printf("EMM Version # %02x\n",(char)regs.h.al);
  152.    }
  153. }
  154.  
  155. static aloc(i)
  156. int i ;
  157. {
  158. unsigned usedp ;
  159.  
  160. regs.x.ax = 0x4300 ;            /* 43h ALLOCATE */
  161. usedp = (freep+1)>>1  ;            /* Take half of what's avail. */
  162. regs.x.bx = usedp ;
  163. int86(0x67, ®s, ®s) ;
  164. if (regs.h.ah) usrexit(0x43,(int)(regs.h.ah)) ;
  165. else {
  166.    printf("%d pages allocated to handle %d\n",
  167.         tusedp[i]=usedp,(thandle[i]=(regs.x.dx))) ;
  168.    freep -= usedp ; 
  169.    }
  170. }
  171.  
  172. static de_aloc(i) 
  173. int i ;
  174. {
  175. unsigned npgs ;
  176.  
  177. regs.h.ah = (char)0x4c ;        /* 4ch get EMM handle pages */
  178. regs.x.dx = thandle[i];
  179. int86(0x67, ®s, ®s) ;
  180. if (regs.h.ah) usrexit(0x4c,(int)(regs.h.ah)) ;
  181. npgs = regs.x.bx ;
  182.  
  183. if (npgs != tusedp[i]) {        /* These should be the same ! */
  184.    printf("VEMM-reported pages for handle %d not same as applications (%d,%d)\n",
  185.         thandle[i], npgs, tusedp[i] );
  186.    usrexit(0x4c,0x80) ;
  187.    }
  188.  
  189. regs.h.ah = (char)0x45 ;        /* 45h DE-ALLOCATE */
  190. regs.x.dx = thandle[i];
  191. int86(0x67, ®s, ®s) ;
  192. if (regs.h.ah) usrexit(0x45,(int)(regs.h.ah)) ;
  193. else {
  194.    printf("[%d] H=%d freed; %d pages.\n",i,thandle[i],npgs) ;
  195.    freep += npgs ;
  196.    thandle[i] = 0;
  197.    tusedp[i] = 0;
  198.    }
  199.  
  200. }
  201.  
  202. static prt_map()
  203. {
  204. if (vdevt!=2) return ;            /* Don't do unless is user interrupt */
  205. regs.x.ax = 0x3F00 ;            /* Print Map                  */
  206. int86(0x67, ®s, ®s) ;
  207. if (regs.h.ah) {
  208.    usrexit(0x3f,(int)(regs.h.ah)) ;
  209.    }
  210. }
  211.  
  212. static usrexit(num,rc)
  213. int num, rc;
  214. {
  215. int k ;
  216.  
  217. printf("EMM[0x%02x] R(0x%02x); Continue(Y/N)? ", num, rc);
  218. while(kbhit()) getch();
  219.  
  220. do {
  221.    while(!kbhit());
  222.    k = getch() ;
  223.    if ((k=toupper(k))=='\r') break;
  224.    } while(k!='Y' && k!='N') ;
  225.  
  226. printf("%c\n",k);
  227. if (k=='N') exit(1000) ;
  228. }
  229.  
  230.  
  231. static more() 
  232. {
  233. printf("\r--more--") ;
  234. while(!kbhit()) ;
  235. while(kbhit()) getch() ;
  236. printf("\r        \r");
  237. }
  238.  
  239. static emm_map(handle, page, window)
  240. int handle, page, window ;
  241. {
  242. regs.h.ah = 0x44 ;            /* Map Handle Pages             */
  243. regs.h.al = (char)(window) ; 
  244. regs.x.dx = handle ;
  245. regs.x.bx = page ;
  246. int86(0x67, ®s, ®s) ;
  247. if (regs.h.ah) {
  248.    usrexit(0x44,(int)(regs.h.ah)) ;
  249.    }
  250. }
  251.  
  252. static use_emm(hslot,size)
  253. int hslot, size;
  254. {
  255. char message[255] ; 
  256. unsigned i, j, o ; 
  257.  
  258. printf("Writing %d sprintf strings to expanded memory handle=%d at segment %04x\n",
  259.         size,thandle[hslot],emm_pfba);
  260.  
  261. for (o=j=i=0; i<size; i++, j++, o+=0x4000 ) {
  262.     if (j>3) {  o = j = 0 ;  }    /* NOTE:  ASSUMES ALL 4 WINDOWS OK TO USE!  */
  263.     sprintf(message,"Hello, from page %d, in window %d (@%04x:%04x)",
  264.         i,j,emm_pfba,o);
  265.     emm_map(thandle[hslot],i,j); 
  266.     poke(emm_pfba,o,message,sizeof(message));
  267.     }
  268.  
  269. printf("Strings have been written.\n");
  270. printf("Reading Strings from EMM buffer:\n");
  271. for (o=j=i=0; i<size; i++, j++, o+=0x4000 ) {
  272.     if (j>3) {  o = j = 0 ;  }    
  273.     emm_map(thandle[hslot],i,j); 
  274.     peek(emm_pfba,o,message,sizeof(message));
  275.     printf("msg[%d.%d]='%s'.\n",i,j,message);
  276.     }
  277.  
  278. }
  279.